package test;

import common.Util;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;

/**
 * @projectName: BlogUIAutoTest
 * @package: test
 * @className: LoginPage
 * @author: Tongxinxin
 * @description: TODO
 * @date: 2025/9/4 15:19
 * @version: 1.0
 */
public class LoginPage extends Util {
    //public String url="http://8.137.19.140:9090/blog_login.html";
    //测试页面可以正常打开
    public void checkPageRight(){
        createDriver();
        driver.get("http://8.137.19.140:9090/blog_login.html");
        //检查菜单
        driver.findElement(By.xpath("/html/body/div[1]"));
        driver.findElement(By.xpath("/html/body/div[2]"));
        //检查登录框
        driver.findElement(By.xpath("/html/body/div[2]/div"));
        driver.findElement(By.xpath("//*[@id=\"username\"]"));
        driver.findElement(By.xpath("//*[@id=\"password\"]"));
        driver.findElement(By.xpath("//*[@id=\"submit\"]"));
    }
    //测试正常登录----正确的账号和密码
    public void checkLogin(){
        createDriver();
        driver.get("http://8.137.19.140:9090/blog_login.html");
        //先清空
        driver.findElement(By.cssSelector("#username")).clear();
        driver.findElement(By.cssSelector("#password")).clear();
        //输入正确的账号和密码
        driver.findElement(By.xpath("//*[@id=\"username\"]")).sendKeys("zhangsan");
        driver.findElement(By.xpath("//*[@id=\"password\"]")).sendKeys("123456");
        driver.findElement(By.xpath("//*[@id=\"submit\"]")).click();
        //如果登录成功会跳转到博客列表页----》有注销按钮
        driver.findElement(By.xpath("/html/body/div[1]/a[3]"));

    }
    /**
     * 异常登录
     * ----用户名和密码都为空
     * ----用户名为空，密码不为空
     * ----用户名不为空，密码为空
     * ----正确的用户名，错误的密码
     * ----错误的用户名，正确的密码
     * ----错误的用户名和密码
     * ----输入框：输入过长的内容、过短的、特殊字符...
     */
    //输入正确的账号，错误的密码
    public void LoginFail1() throws InterruptedException {
        createDriver();
        driver.get("http://8.137.19.140:9090/blog_login.html");
        //先清空
        driver.findElement(By.cssSelector("#username")).clear();
        driver.findElement(By.cssSelector("#password")).clear();

        driver.findElement(By.cssSelector("#username")).sendKeys("zhangsan");
        driver.findElement(By.cssSelector("#password")).sendKeys("123");
        driver.findElement(By.cssSelector("#submit")).click();

        //处理错误弹窗——警告弹窗
        Thread.sleep(500);
        Alert alert = driver.switchTo().alert();
        alert.accept();
    }
    //输入错误的账号，正确的密码
    public void LoginFail2() throws InterruptedException {
        createDriver();
        driver.get("http://8.137.19.140:9090/blog_login.html");
        //先清空
        driver.findElement(By.cssSelector("#username")).clear();
        driver.findElement(By.cssSelector("#password")).clear();

        driver.findElement(By.cssSelector("#username")).sendKeys("zhangsan111");
        driver.findElement(By.cssSelector("#password")).sendKeys("123456");
        driver.findElement(By.cssSelector("#submit")).click();

        //处理错误弹窗——警告弹窗
        Thread.sleep(500);
        Alert alert = driver.switchTo().alert();
        alert.accept();
    }
    //输入错误的账号，错误的密码
    public void LoginFail3() throws InterruptedException {
        createDriver();
        driver.get("http://8.137.19.140:9090/blog_login.html");
        //先清空
        driver.findElement(By.cssSelector("#username")).clear();
        driver.findElement(By.cssSelector("#password")).clear();

        driver.findElement(By.cssSelector("#username")).sendKeys("zhangsan1111");
        driver.findElement(By.cssSelector("#password")).sendKeys("123");
        driver.findElement(By.cssSelector("#submit")).click();

        //处理错误弹窗——警告弹窗
        Thread.sleep(500);
        Alert alert = driver.switchTo().alert();
        alert.accept();
    }
    //输入空的账号，空的密码
    public void LoginFail4() throws InterruptedException {
        createDriver();
        driver.get("http://8.137.19.140:9090/blog_login.html");
        //先清空
        driver.findElement(By.cssSelector("#username")).clear();
        driver.findElement(By.cssSelector("#password")).clear();

        driver.findElement(By.cssSelector("#username")).sendKeys("");
        driver.findElement(By.cssSelector("#password")).sendKeys("");
        driver.findElement(By.cssSelector("#submit")).click();

        //处理错误弹窗——警告弹窗
        Thread.sleep(500);
        Alert alert = driver.switchTo().alert();
        alert.accept();
    }
}
